package leetcode.N1_N100;

import java.util.Arrays;

/**
 * @author xiwai [hww282306@alibaba-inc.com]
 * @version 1.0
 * @date 2020/10/19 2:24 下午
 */
public class N14 {


    public String longestCommonPrefix(String[] strs) {
        int length = strs.length;
        if (length == 0) {
            return "";
        }
        int index = 0;
        int minLength = strs[index].length();
        // 遍历找出最短字符串下标
        for (int i = 1; i < length; i++) {
            int currentLength = strs[i].length();
            if (currentLength < minLength) {
                minLength = currentLength;
                index = i;
            }
        }
        // 找到最小样
        char[] sample = strs[index].toCharArray();
        int effectiveLength = sample.length;

        // 遍历比较相同的字符串
        for (String str : strs) {
            effectiveLength = getPrefix(sample, str.toCharArray(), effectiveLength);
            if (effectiveLength == 0) {
                return "";
            }
        }
        return String.copyValueOf(sample, 0, effectiveLength);
    }

    private int getPrefix(char[] left, char[] right, int effectiveLength) {
        effectiveLength = Math.min(effectiveLength, right.length);
        for (int i = 0; i < effectiveLength; i++) {
            if (left[i] != right[i]) {
                return i;
            }
        }
        return effectiveLength;
    }


    public static void main(String[] args) {
        char[] a = {'a', 'b', 'c'};
        System.out.println();


    }

}
